package com.lq.controller;

import com.lq.pojo.User;
import com.lq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session,Model model) {

        User user = userService.login(username, password);

        if (user == null){
            return "login";
        }

        session.setAttribute("user",user);

        return "forward:/panel";
    }

    @RequestMapping("/quit")
    public String login(HttpSession session) {

        session.removeAttribute("user");

        return "login";
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    @RequestMapping("/doRegister")
    public String doRegister(String un,String up,String ut,String us ,Model model) {

        //判断用户昵称是否存在
        if (userService.selectCountByName(un) == 0){

            userService.insertUser(un, up,ut, us);

        }else {
            model.addAttribute("nameIsExists",true);
        }

        return "register";
    }
}